﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class sddichvu : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("phong");
        private DataTable dt1 = new DataTable("dichvu");
        private DataTable dt2 = new DataTable("dkdichvu");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open();

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public sddichvu()
        {
            InitializeComponent();
        }

        private void sddichvu_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            
        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from phong where pstatus = 1";
            da.SelectCommand = command;
            da.Fill(dt);
            cbbphong.DataSource = dt;
            cbbphong.DisplayMember = "pid";
            cbbphong.ValueMember = "pid";

            command.CommandText = @"select * from dichvu";
            da.SelectCommand = command;
            da.Fill(dt1);
            showdichvu.DataSource = dt1;
            //showdichvu.AutoResizeColumn();
            cbbdichvu.DataSource = dt1;
            cbbdichvu.DisplayMember = "dvtendichvu";
            cbbdichvu.ValueMember = "dvid";
            cbbdichvu.DataBindings.Clear();
            cbbdichvu.DataBindings.Add("Text",showdichvu.DataSource,"dvtendichvu");

            command.CommandText = @"Select * from dkdichvu";
            da.SelectCommand = command;
            da.Fill(dt2);

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            dichvu qlp = new dichvu();
            qlp.Show();
        }

        private void btndangky_Click(object sender, EventArgs e)
        {
            DataRow row = dt2.NewRow();
            row["dkdvsophong"] = cbbphong.SelectedValue;
            row["dvid"] = cbbdichvu.SelectedValue;
            dt2.Rows.Add(row);
            SqlCommand commandInsert = new SqlCommand();
            commandInsert.Connection = conn;
            commandInsert.CommandType = CommandType.Text;
            commandInsert.CommandText = @"Insert into dkdichvu values(@sophong,@madv)";
            commandInsert.Parameters.Add("@sophong",SqlDbType.Int,20,"dkdvsophong");
            commandInsert.Parameters.Add("@madv",SqlDbType.Int,20,"dvid");
            da.InsertCommand = commandInsert;
            da.Update(dt2);
            Dispose();
            dichvu dv = new dichvu();
            dv.Show();
        }
    }
}
